#include "AppMenu.h"
#include "AppMenuPanel.h"
#include <widgets/SImageButton.h>
#include <QBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QApplication>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include "core/StyleMgr.h"

AppMenu::AppMenu(MainFrame* frame, QWidget* parent)
    : QWidget(parent),
      m_frame(frame) {
 	m_btnLogo = new SImageButton(this, "guiframe_app_logo_title");
	m_btnLogo->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    m_btnTxt = new  SImageButton(this, "guiframe_app_logo_title_text");
	m_btnTxt->setAttribute(Qt::WA_TransparentForMouseEvents, true);
	QColor clr = NAME_COLOR("TextNormal");
    QLabel* label = new QLabel(tr("菜单"), this);
    label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    label->setAlignment(Qt::AlignCenter);
    //label->setStyleSheet("color:rgb(255,241,125);");
    label->setStyleSheet(QString("color:rgb(#%1);").arg(clr.rgba(), 0, 16));
	label->setAttribute(Qt::WA_TransparentForMouseEvents, true);

    QHBoxLayout* layout = new QHBoxLayout();
    layout->addWidget(m_btnLogo);
    layout->addWidget(m_btnTxt);
    layout->addWidget(label);
    layout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
    setLayout(layout);

    m_appMenu = new AppMenuPanel(this);
    m_appMenu->setVisible(false);
    connect(this, &AppMenu::setCommonPage, m_appMenu, &AppMenuPanel::onCommonPage);
}

AppMenu::~AppMenu() {
}

bool AppMenu::eventFilter(QObject* watched, QEvent* e){
    if (e->type() == QEvent::MouseMove){
        if (m_appMenu->isVisible()) {
			QRect myRect(mapToGlobal(rect().topLeft()), QSize(rect().width(), rect().height()));
			QRect menuPanelRect(m_appMenu->mapToGlobal(QPoint(0, 0)), m_appMenu->size());
			QPoint pos = QCursor::pos();
			if (!myRect.contains(pos) && !menuPanelRect.contains(pos)) {
				m_timerHideMenu = startTimer(500);
			}
        } else {
			KillHideMenuTimer();
        }
    }
    return QWidget::eventFilter(watched, e);
}

void AppMenu::enterEvent(QEvent* event){
    if (!m_appMenu->isVisible()) {
		KillShowMenuTimer();
		m_timerShowMenu = startTimer(1000);
    }
}

void AppMenu::leaveEvent(QEvent* event) {
	KillShowMenuTimer();
}

void AppMenu::mousePressEvent(QMouseEvent* event){
	QWidget::mousePressEvent(event);
}

void AppMenu::mouseReleaseEvent(QMouseEvent* event) {
    if (!m_appMenu->isVisible()) {
		KillShowMenuTimer();
        onShowMenu();
    }
	QWidget::mouseReleaseEvent(event);
}

void AppMenu::timerEvent(QTimerEvent* e)
{
    if (e->timerId() == m_timerShowMenu) {
		KillShowMenuTimer();
        onShowMenu();
    } else if (e->timerId() == m_timerHideMenu) {
        KillHideMenuTimer();
        onHideMenu();
    }
}

void AppMenu::onShowMenu() {
    if (!m_appMenu->isVisible()) {
		QRect myRect(mapToGlobal(rect().topLeft()), QSize(rect().width(), rect().height()));
		QRect menuPanelRect(m_appMenu->mapToGlobal(QPoint(0, 0)), m_appMenu->size());
		QPoint pos = QCursor::pos();
		if (myRect.contains(pos) || menuPanelRect.contains(pos)) {
			m_appMenu->move(mapToGlobal(rect().topLeft()));
			m_appMenu->show();
			qApp->installEventFilter(this);
		}
    }
}

void AppMenu::onHideMenu() {
    if (m_appMenu->isVisible()) {
		m_appMenu->hide();
    }
	qApp->removeEventFilter(this);
}

void AppMenu::KillShowMenuTimer() {
	if (m_timerShowMenu != -1) {
		killTimer(m_timerShowMenu);
		m_timerShowMenu = -1;
	}
}

void AppMenu::KillHideMenuTimer() {
	if (m_timerHideMenu != -1) {
		killTimer(m_timerHideMenu);
		m_timerHideMenu = -1;
	}
}

void AppMenu::onCommonPage(int* ids, int size, int ntype){
    setCommonPage(ids, size, ntype);
}
